package com.atguigu.wordcount.mapjoin;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.*;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ReducerJoinMapper
 * @DeScription ReducerJoinMapper
 * @Author 剧情再美终是戏
 * @Date 2019/11/20 11:36
 * @Version 1.0
 **/

public class MapJoinMapper extends Mapper<LongWritable, Text, NullWritable, JoinBean> {

    private JoinBean joinBean = new JoinBean();

    private Map<String, String> pdMap = new HashMap();

    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        URI[] uri = context.getCacheFiles();
        for (URI uri1 : uri) {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(uri1))));
            String line = null;
            while (StringUtils.isNotEmpty(line = bufferedReader.readLine())) {
                String[] split = line.split("\t");
                pdMap.put(split[0], split[1]);
            }
        }
    }

    // order.txt 1001	01	1
    // pd.txt 01	小米
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] split = value.toString().split("\t");

        joinBean.setaMount(split[2]);
        joinBean.setOrderId(split[0]);
        joinBean.setpName(pdMap.get(split[1]));
        context.write(NullWritable.get(), joinBean);
    }
}
